// 实现这个项目的构建任务
const {src, dest, parallel, series, watch} = require('gulp')

const browserSync = require('browser-sync')
const bs = browserSync.create()

const loadPlugins = require('gulp-load-plugins')
const plugins = loadPlugins() // 自动引入插件

const sass = require('gulp-sass')(require('sass')) // 老师能在评语里说一下这是种什么写法吗，我构建的时候它提示要这么写，
                                                  // 说是因为sass版本5没有sass这个模块，需要自定义一个
const del = require('del')

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// temp （这里的文件会被 useref 影响）
const style = () => {
  return src('src/assets/styles/*.scss', {base: 'src'})
    .pipe(sass({outputStyle: 'expanded'}))
    .pipe(dest('temp'))
}

const script = () => {
  return src('src/assets/scripts/*.js', {base: 'src'})
    .pipe(plugins.babel({presets: ['@babel/preset-env']}))
    .pipe(dest('temp'))
}

const html = () => {
  return src('src/*.html', {base: 'src'})
    .pipe(plugins.swig({data}))
    .pipe(dest('temp'))
}

// dist
const image = () => {
  return src('src/assets/images/**', {base: 'src'})
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', {base: 'src'})
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const extra = () => {
  return src('public/**', {base: 'public'})
    .pipe(dest('dist'))
}

const clean = () => {
  return del(['dist', 'temp'])
}

const useref = () => {
  return src('temp/*.html', {base: 'dist'})
    .pipe(plugins.useref({searchPath: ['dist', '.']}))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      collapseWhitespace: true,
      minifyCSS: true,
      minifyJS: true
    })))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(dest('dist'))
}

const serve = () => {
  watch('src/*.html', html)
  watch('src/assets/styles/*.scss', style)
  watch('src/assets/scripts/*.js', script)
  watch([
    'src/assets/images/**',
    'src/assets/fonts/**',
    'public/**'
  ], bs.reload)

  bs.init({
    notify: false,
    port: 8080,
    files: 'dist/**',
    server: {
      baseDir: ['temp', 'src', 'public'],
      routes: {
        '/node_modules': 'node_modules'
      },
    }
  })
}

const compile = parallel(html, style, script)
const build = series(
  clean,
  parallel(
    series(compile, useref),
    image,
    font,
    extra
  ))

const develop = series(compile, serve)

module.exports = {
  build,
  clean,
  serve,
  develop,
}
